package com.example.demo.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import com.example.demo.Exception.ProcessException;

/**
 * 测试邮件发送功能
 */
@Service
public class MailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送纯文本邮件
     * @param to            邮件接收方
     * @param subject       邮件主题
     * @param text          邮件内容
     */
    public void sendTextMail(String to,String subject,String text)throws Throwable{
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(text);

        try{
            javaMailSender.send(simpleMailMessage);
        }catch (Exception e){
            ProcessException pe= new ProcessException(e.getMessage(),-1);
            throw pe;
        }
    }
}
